import treeSelectOne from "./treeSelectOne.vue";
export default {
	data() {
		return {
			chooseIds: "",
			chooseItems: "",
			listData: [],
			search: {
				timer: null,
				key: ''
			}
		};
	},
	props: {
		data: Array,
		keywordsFilter: {
			type: Boolean
		}
	},
	components: {
		treeSelectOne,
	},
	created() {
		this.listData = JSON.parse(JSON.stringify(this.data));
		// console.log(this.listData)
	},
	methods: {
		KeywordFilter2() {
			console.log(1212222)
		},
		KeywordFilter1(a) {
			if (this.search.timer !== null) {
				clearTimeout(this.search.timer);
			}
		},
		KeywordFilter() {
			console.log(this.search.key)
			this.listData = JSON.parse(JSON.stringify(this.data));
			
			if (this.search.timer !== null) {
				clearTimeout(this.search.timer);
			}
			this.search.timer = setTimeout(() => {
				this.getKeyword();
			}, 300);
		},
		getKeyword() {
			if (this.search.key === undefined || this.search.key === null || this.search.key === '') {
				this.listData = JSON.parse(JSON.stringify(this.data));
			} else {
				this.listData = this.treeFilter(this.listData, this.search.key);
			}
			this.showChooseResult();
		},
		// 回显展示效果
		showChooseResult() {
			if (!this.ids) return;
			console.log(this.listData)
			console.log(this.search.key)
			console.log(this.ids)
			this.chooseIds = this.ids;
			this.chooseItems = [];
			// 单选
			if (!this.multiple) {
				this.changeValue(this.listData, JSON.parse(JSON.stringify(this.chooseIds)));
			} else {
				const chooseIds = JSON.parse(JSON.stringify(this.chooseIds));
				chooseIds.forEach((item) => {
					this.changeValue(this.listData, item);
				});
			}

			// 回显问题。
			if (this.chooseIds.length !== 0 && this.listData.length > 0) {
				if (!this.multiple) {
					const parents = getTreeParents(this.listData, this.ids[this.ids.length - 1], this.props.id, []);
					this.ljs_tree1.departmentName = "";
					parents.forEach((item, i) => {
						this.ljs_tree1.departmentName += item[this.props.label] + "-";
					});
					this.ljs_tree1.departmentName = this.ljs_tree1.departmentName.substring(0, this.ljs_tree1
						.departmentName
						.length - 1);
				}
			}
		},
		// 关闭
		close() {
			this.$emit("close", false);
			this.chooseIds = [];
			this.chooseItems = [];
			this.initValue(this.data);
		},
		// 选中的值
		save() {
			if (this.chooseIds.length > 0) {
				this.$emit("getChooseValue", this.chooseIds, this.chooseItems); // 继续向上传递
				// this.close();
			} else {
				console.log(this)
				// this.BASE.msg("您还没有选择任何项目。");
				uni.showToast({
					title: '您还没有选择任何项目。',
					icon: 'none'
				});
			}
		},
		treeFilter(tree, keywords) {
			const result = [];
			if (tree === undefined || tree === null || tree === '') {
				return tree;
			}
			const nums = tree.length;
			for (let i = 0; i < nums; i++) {
				const item = tree[i];
				if (item["label"].includes(keywords)) {
					item.openTag = true;
					result.push(item);
					continue;
				}
				if (item["children"] !== undefined && item["children"].length > 0) {
					item["children"] = this.treeFilter(item["children"], keywords);
					if (item["children"].length > 0) {
						item.openTag = true;
						result.push(item);
					}
				}
			}
			return result;
		},
		// 获取tree选择的结果
		// ids的数组
		// items对象数组
		getValue(ids, items) {
			this.chooseIds = ids.reverse();
			this.chooseItems = items.reverse();
			this.changeValue(
				this.listData,
				JSON.parse(JSON.stringify(this.chooseIds))
			);
		},
		// 递归改值
		changeValue(data, chooseIds) {
			data.forEach((item, i) => {
				item.choose =
					chooseIds[chooseIds.length - 1] === item.id ?
					item.choose ?
					false :
					true :
					false;
				// 小程序选择bug优化。
				if (chooseIds[0] === item.id) {
					chooseIds.splice(0, 1);
					item.openTag = true;
				}

				if (item.children !== undefined) {
					this.changeValue(item.children, chooseIds);
				}
			});
		},
		// 初始化
		initValue(data) {
			data.forEach((item, i) => {
				item.choose = false;
				item.openTag = false;
				if (item.children !== undefined) {
					this.initValue(item.children);
				}
			});
		},
	},
};